package com.bosi.exmple.chat.handler;

import com.bosi.exmple.chat.ChatWebServerSocket;
import com.bosi.exmple.chat.SessionManager;
import com.bosi.exmple.chat.entity.User;
import com.bosi.exmple.chat.entity.WSRequest;
import com.bosi.exmple.chat.entity.WSResponse;
import com.bosi.exmple.message.Message;
import com.bosi.exmple.message.MessageService;
import com.bosi.ws.server.WebSocketSession;

import java.util.HashMap;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * 打开或关闭连接
 * <p>
 * 注册频道或关闭频道
 */
public class OpenOrCloseHandler implements Function<WSRequest, Object> {
    private final static String DEF_GAME = "def_game";
    private final static String TOKEN = "token";

    private final static String USER = "user";
    private final static String GAME = "game";
    private final static String SYS = "sys";
    private final static String HALL = "hall";

    private final MessageService messageService;

    public OpenOrCloseHandler(MessageService messageService) {
        this.messageService = messageService;
    }

    @Override
    public Object apply(WSRequest wsRequest) {
        WebSocketSession session = wsRequest.getSession();
        Object user = session.get(USER);
        if (user == null) {
            open(session);
        } else {
            close(session);
        }
        return null;
    }

    private void close(WebSocketSession session) {
        User user = (User) session.get(USER);
        SessionManager.remove(user.getId());
        messageService.unsubscribe(user.getWorldId());
        messageService.unsubscribe(user.getHallId());
        messageService.unsubscribe(user.getSysId());

    }

    private void open(WebSocketSession session) {
        User user = new User();
        user.setId(session.getParam("id"));
        user.setGame(session.getParam("game"));
        user.setHall(session.getParam("hall"));

        SessionManager.put(user.getId(), session);

        if (user.getGame() == null) {
            user.setGame(DEF_GAME);
        }

        session.put(USER, user);

        //世界频道
        HashMap<String, Object> worldHeader = new HashMap<>();
        worldHeader.put(GAME, user.getGame());
        Consumer<Message> messageConsumer = message -> {
            WSResponse res = WSResponse.createMessage(message);
            ChatWebServerSocket.sendMessage(res, session);
        };
        String worldId = messageService.subscribe(worldHeader, messageConsumer);
        user.setWorldId(worldId);

        //系统频道
        HashMap<String, Object> sysHeader = new HashMap<>();
        sysHeader.put(GAME, user.getGame());
        sysHeader.put(SYS, user.getGame());
        String sysId = messageService.subscribe(sysHeader, messageConsumer);
        user.setSysId(sysId);

        //大厅频道
        if (user.getHall() != null) {
            HashMap<String, Object> hallHeader = new HashMap<>();
            hallHeader.put(GAME, user.getGame());
            hallHeader.put(HALL, user.getHallId());
            String hallId = messageService.subscribe(hallHeader, messageConsumer);
            user.setHallId(hallId);
        }

        //私人频道直接发送,无需注册
    }

}
